package com.ccpdown.util.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class SocketContent {

	public String getContent(String path) {
		String str = null;
		InputStream in = null;
		try {
			URL url = new URL(path); // 创建一个 URL 对象
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection(); // 创建一个 URL 连接，如果有代理的话可以指定一个代理。
			connection.setConnectTimeout(60000); // 在开始和服务器连接之前，可能需要设置一些网络参数
													// 连接超时时间；
			connection.connect();
			in = connection.getInputStream(); // 向服务器发送数据并获取应答
			str=inputStream2String(in);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return str;
	}
	
	public String inputStream2String(InputStream is) throws IOException{
	    BufferedReader in = new BufferedReader(new InputStreamReader(is));
	    StringBuffer buffer = new StringBuffer();
	    String line = "";
	    while ((line = in.readLine()) != null){
	      buffer.append(line);
	    }
	    return buffer.toString();
	}
}
